/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    can.c
 * @brief   This file provides code for the configuration
 *          of the CAN instances.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "BSP_CAN.h"
#include "motor_control.h"
#include "control_config.h"
#include "loop.h"
#include "signal_port.h"
/* USER CODE BEGIN 0 */
#if CAN_RX0_INT_ENABLE /* 使能RX0中断 */

#define MAX_Current 2500

/**
 * @brief       CAN RX0 中断服务函数
 *   @note      处理CAN FIFO0的接收中断
 * @param       无
 * @retval      无
 */
void CAN0_RX1_IRQHandler()
{
    static int16_t * dec_param_reg = (int16_t *)&g_motor_config;
    int16_t param;
    uint8_t function = 0;//操作码

    // static uint8_t i = 0, R_stage = 0, Parameters[2][8] = {0};
    /* 检查接收中断标志 */
    if(can_interrupt_flag_get(CAN0, CAN_INT_FLAG_RFF0) == RESET) {
        return;
    }

    /* 接收报文 */
    can_message_receive(CAN0, CAN_FIFO0, &bsp_can.CanRx);
    can_interrupt_flag_clear(CAN0, CAN_INT_FLAG_RFF0);

    if (bsp_can.CanRx.rx_ff != CAN_FF_STANDARD || bsp_can.CanRx.rx_ff != CAN_FT_DATA) /* 不是标准帧 / 不是数据帧 */
    {
        return;
    }
    if (bsp_can.CanRx.rx_sfid & 0xff == DEVICE_ID)
    {
        function = (bsp_can.CanRx.rx_sfid >> 8) & 0xF;    // 操作码
        switch (function)
        {
            // ==================== 读操作 ====================
            case 0x01:
                switch (bsp_can.CanRx.rx_data[0]) //功能码
                {
                    /* 获取电机运行模式 */
                    case MOTOR_Mode_GET:
                    {
                        bsp_can.CanRx.rx_data[0] = MOTOR_Mode_GET;
                        bsp_can.CanRx.rx_data[1] = motor_control.mode_order;
                        //bsp_can.CanRx.rx_data[2] = stall_mode;
                        can_send_msg(0x01, bsp_can.CanRx.rx_data, 2);
                        break;
                    }
                    /**
                     * DCE控制参数读取
                     */
                    case MOTOR_DCE_PARAM_GET:
                    {
                        bsp_can.CanRx.rx_data[0] = MOTOR_DCE_PARAM_GET;
                        bsp_can.CanRx.rx_data[1] = bsp_can.CanRx.rx_data[1];
                        switch (bsp_can.CanRx.rx_data[1])
                        {
                            case 0:
                                bsp_can.CanRx.rx_data[2] = (uint8_t)(dce.kp >> 8);
                                bsp_can.CanRx.rx_data[3] = (uint8_t)(dce.kp);
                                break;
                            case 1:
                                bsp_can.CanRx.rx_data[2] = (uint8_t)(dce.ki >> 8);
                                bsp_can.CanRx.rx_data[3] = (uint8_t)(dce.ki);
                                break;
                            case 2:
                                bsp_can.CanRx.rx_data[2] = (uint8_t)(dce.kv >> 8);
                                bsp_can.CanRx.rx_data[3] = (uint8_t)(dce.kv);
                                break;
                            case 3:
                                bsp_can.CanRx.rx_data[2] = (uint8_t)(dce.kd >> 8);
                                bsp_can.CanRx.rx_data[3] = (uint8_t)(dce.kd);
                                break;
                            default:
                                break;
                        }
                        can_send_msg(0x01, bsp_can.CanRx.rx_data, 4);
                        break;
                    }
                    /**
                     * 电流参数读取(额定电流，校准电流)
                     */
                    case CONTROL_CONFIG_GET:
                    {
                        bsp_can.CanRx.rx_data[0] = CONTROL_CONFIG_GET;
                        bsp_can.CanRx.rx_data[1] = (uint8_t)(dec_param_reg[0] >> 8);
                        bsp_can.CanRx.rx_data[2] = (uint8_t)(dec_param_reg[0]);
                        bsp_can.CanRx.rx_data[3] = (uint8_t)(dec_param_reg[1] >> 8);
                        bsp_can.CanRx.rx_data[4] = (uint8_t)(dec_param_reg[1]);
                        can_send_msg(0x01, bsp_can.CanRx.rx_data, 4);
                        break;
                    }
                    default:
                        break;
                }
                break;
            // ==================== 写操作 ====================
            case 0x02:
                switch (bsp_can.CanRx.rx_data[0]) //功能码
                {
                    // 更改电机的模式
                    case MOTOR_Mode_SET:
                    {
                        // motor_control.mode_order = bsp_can.rx_data[1];
                        // cricle_open_switch = bsp_can.rx_data[2];
                        // if (cricle_open_switch == 0) // 闭环初始化
                        // {
                        //     // 初始化EN DIR PUL
                        //     /* code */
                        // }
                        if (cricle_open_switch == bsp_can.CanRx.rx_data[1])
                        {
                            if (cricle_open_switch == OPEN)
                                break;
                            if (motor_control.mode_run == bsp_can.CanRx.rx_data[2])
                                break;
                            else motor_control.mode_order = bsp_can.CanRx.rx_data[1];
                        }
                        else
                        {
                            cricle_open_switch = bsp_can.CanRx.rx_data[1];
                            switch (cricle_open_switch)
                            {
                            case OPEN:
                                /* DeInit GPIO  Init GPIO */
                                //清除当前模式
                                switch(signal_moreio.mode)
                                {
                                    case MoreIO_Mode_Disable:                                                break;        //Disable
                                    case MoreIO_Mode_PWM_Location:            Signal_PWM_DeConfig();        break;        //PWM位置
                                    case MoreIO_Mode_PWM_Speed:                    Signal_PWM_DeConfig();        break;        //PWM速度
                                    case MoreIO_Mode_PWM_Current:                Signal_PWM_DeConfig();        break;        //PWM电流
                                    case MoreIO_Mode_PULSE_Locatioon:        Signal_Count_DeConfig();    break;        //PULSE位置
                                }
                                //初始化 开环模式
                                // OPEN_GPIO_Init();
                                break;
                            case CIRCLE:
                                /* DeInit GPIO  Init GPIO mode_run = bsp_can.rx_data[2] */
                                // OPEN_GPIO_DeInit();
                                // Motor_Control_Init();
                                motor_control.mode_order = bsp_can.CanRx.rx_data[2];
                                break;
                            default:
                                break;
                            }
                        }

                        // stall_mode = bsp_can.rx_data[3];
                        break;
                    }
                    // 设置DCE控制参数
                    /***
                     * rx_data[2] = 00 : dce.kp 01 : dce.ki 02 : dce.kv 03 : dce.kd
                     */
                    case MOTOR_DCE_PARAM_SET:
                    {
                        param = (bsp_can.CanRx.rx_data[2] << 8);
                        param += bsp_can.CanRx.rx_data[3];
                        switch (bsp_can.CanRx.rx_data[1])
                        {
                            case 0:
                                Control_DCE_SetKP(param);
                                break;
                            case 1:
                                Control_DCE_SetKI(param);
                                break;
                            case 2:
                                Control_DCE_SetKV(param);
                                break;
                            case 3:
                                Control_DCE_SetKD(param);
                                break;
                        }
                        break;
                    }
                    // 更改控制config参数
                    /**
                     * 额定电流、校准电流、细分数、额定转速、固件额定加速加速度
                     * 固件额定减速加速度、固件额定增流梯度、固件额定减流梯度
                     */
                    case CONTROL_CONFIG_SET:
                    {
                        param = (bsp_can.CanRx.rx_data[2] << 8);
                        param += bsp_can.CanRx.rx_data[3];
                        if (param <= MAX_Current)
                        {
                            (*(dec_param_reg + bsp_can.CanRx.rx_data[1])) = param;
                        }
                        can_send_msg(0x01, (uint8_t *)(dec_param_reg + bsp_can.CanRx.rx_data[1]), 2);
                        break;
                    }
                }
                break;
            default:
                // 错误操作码 待完善处理操作
                break;

        }
    }
    // switch (bsp_can.rx_data[0])  //////////////////////////////////协议拆包写寄存器
    // {
    //     // 更改电机的CAN ID信息
    //     case MOTOR_ID:
    //     {
    //         bsp_can.motorsta = MOTOR_ID;
    //         break;
    //     }
    //     // 更改电机的速度
    //     case MOTOR_Speed:
    //     {
    //         // 200(Move_Step_NUM单圈步数)*256(Move_Divide_NUM软件细分数)*50(额定50转/s) = 2,560,000
    //         // BIN:0010 0111 0001 0000 0000 0000       HEX:02 07 01 00 00 00    OCT:11 61 00 00    (CAN的数据范围00 00 00 00 - 11 61 00 00)
    //         // 1转/s    51200对应OCT为: 00 14 40 00
    //         // int32范围：-2,147,483,6418到2,147,483,647
    //         bsp_can.motorsta = MOTOR_Speed;
    //         bsp_can.goal_speed = bsp_can.rx_data[1] << 24;
    //         bsp_can.goal_speed += bsp_can.rx_data[2] << 16;
    //         bsp_can.goal_speed += bsp_can.rx_data[3] << 8;
    //         bsp_can.goal_speed += bsp_can.rx_data[4];
    //         motor_control.goal_speed = bsp_can.goal_speed;
    //         break;
    //     }
    //     // 更改电机的位置
    //     case MOTOR_Post:
    //     {
    //         // 1圈 51200对应OCT为: 00 14 40 00
    //         // HEX: 00 00 C8 00
    //         bsp_can.motorsta = MOTOR_Post;
    //         bsp_can.goal_location = bsp_can.rx_data[1] << 24;
    //         bsp_can.goal_location += bsp_can.rx_data[2] << 16;
    //         bsp_can.goal_location += bsp_can.rx_data[3] << 8;
    //         bsp_can.goal_location += bsp_can.rx_data[4];
    //         motor_control.goal_location = bsp_can.goal_location;
    //         break;
    //     }
    //     case MOTOR_Track:
    //     {
    //         // 1圈 51200对应OCT为: 00 14 40 00
    //         // HEX: 00 00 C8 00
    //         bsp_can.motorsta = MOTOR_Track;
    //         bsp_can.goal_location = bsp_can.rx_data[1] << 24;
    //         bsp_can.goal_location += bsp_can.rx_data[2] << 16;
    //         bsp_can.goal_location += bsp_can.rx_data[3] << 8;
    //         bsp_can.goal_location += bsp_can.rx_data[4];
    //         break;
    //     }
    // default:
    //     break;
    // }
}

#endif



/* USER CODE END 0 */
